var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var httpget = require('http');
var bodyParser = require('body-parser');
var session = require('express-session');
var querystring = require('querystring');


app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());

app.use(session({
    secret: 'secret',
    resave: true,
    saveUninitialized: false,
    cookie: {
        maxAge: 1000 * 60 * 10 //过期时间设置(单位毫秒)
    }
}));


app.use(express.static('.'));

app.get('/', function (req, res) {
     res.redirect('/index.html');
});


io.on('connection', function (socket) {

    socket.on('disconnect', function () {
        console.log("client exit:"+socket.id);
    });
    ////////////////////////////////


//即时数据
    socket.on('get_data_now', function () {
        console.log("client want data");
        socket.emit('data_now', g_data);
    });
//历史数据 
    socket.on('get_data_history', function (data) {
    });
    socket.on('getconfig', function () {
        console.log("client want get camera config");
        var rtmpserver = { server: "test", port: "1120"};
        socket.emit('config', "111", "222");
    });

  
    //客户端要求播放流,权限处理
    socket.on('startstream', function (request) {
        console.log("client ask stream %s", request.item);
        socket.join(request.item);
    });
    socket.on('stopstream', function (request) {
        socket.leave(request.item);
    });
    socket.on('streamdata', function (data) {
        let item = data.item;
        socket.broadcast.to(item).emit('data',{item:item,data:data.data});
    });
    //询问网关状态
    socket.on('gateway_status', function (request) {
        let gid = request.gid;
    });

    //局域网内启动转发服务
    socket.on('localnetwork_rrstream', function (data) {

    });
});



http.listen(8080, function () {
    console.log('listening on *:8080');
});


/*
app.get('/camera', function (req, res) {
    res.sendFile(__dirname + '/public/camera.html');
});

app.get('/server', function (req, res) {
    res.sendFile(__dirname + '/public/server.html');
});
app.get('/com', function (req, res) {
    res.sendFile(__dirname + '/public/com.html');
});
app.get('/data', function (req, res) {
    res.sendFile(__dirname + '/public/data.html');
});

app.get('/datahis', function (req, res) {
    res.sendFile(__dirname + '/public/datahistory.html');
});


app.get('/login', function (req, res) {
    res.sendFile(__dirname + '/public/login.html');
});
app.get('/campreview', function (req, res) {
    res.sendFile(__dirname + '/public/CameraIntranet.html');
});

app.get('/log', function (req, res) {
    res.sendFile(__dirname + '/public/log.html');
});
*/

app.post('/login', function (req, res) {
    var user ="test";
    if (req.body.username == user.username && req.body.password == user.password) {
        req.session.user = user;
        res.sendStatus(200);
    } else {
        res.sendStatus(404);
    }
});

//get示例
app.get('/data', function (req, res) {
    //console.log("tcpdataresponse %s", req.body.limit);
    
    pool.getConnection(function (err, connection) {
        if (err) {
            console.log('connect failure');
        } else {
            console.log('connect success');
            connection.query('select * from sensor order by id desc',function (err, result) {
                if (err) {
                    //connection.query("select * from sensor")
                    connection.release();
                } else {
                    //console.log(result);
                    //var data = "[{";
                    res.send(result);
                    connection.release();
                }
            })
        }
    });
});

//post示例
app.post('/tcpdata', function (req, res) {
    console.log(req.body);
    _global_temp = req.body.temp;
    _global_press = req.body.press;
    _global_time = req.body.time;

    console.log("temp:%s,press:%s,time:%s", _global_temp, _global_press, _global_time);
    res.sendStatus(200);
});



app.get('/logout', function (req, res) {
    req.session.user = null;
    req.session.error = null;
    res.redirect('/login');
});
